/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.common;

import cn.ac.iie.di.wx.configuration.Configuration;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.log4j.Logger;

/**
 *
 * @author AlexMu
 */
public class RuntimeEnv {

    private static Configuration conf = null;
    public static final String RECONNECTION = "reconnection";
    public static final String SSBUILDER = "sendSessionBuilder";
    public static final String ADDCOLUMN = "addColumn";
    public static final String RFFPATH = "readFromFilePath";
    public static final String FNAME = "fileName";
    public static final String DOCSNAME = "docSchemaName";
    public static final String LOCALHOST = "localhost";

    private static Map<String, Object> dynamicParams = new HashMap<String, Object>();
    static Logger logger = Logger.getLogger(RuntimeEnv.class.getName());

    public static boolean initialize(Configuration pConf) throws Exception {

        if (pConf == null) {
            
            logger.error("configuration object is null");
            throw new Exception("configuration object is null");
        }

        conf = pConf;

        setKeyAndValue(RECONNECTION, "reconnection");
        setKeyAndValue(SSBUILDER, "sendSessionBuilder");
        setKeyAndValue(ADDCOLUMN, "addColumn");
        setKeyAndValue(RFFPATH, "readFromFilePath");
        setKeyAndValue(FNAME, "fileName");
        setKeyAndValue(DOCSNAME, "docSchemaName");
        setKeyAndValue(LOCALHOST, "localhost");
        return true;
    }

    public static boolean setKeyAndValue(String KEY, String parameter) throws Exception {
        String value = conf.getString(KEY, "");
        if (value.isEmpty()) {
            logger.error("parameter " + parameter + " does not exist or is not defined");
            throw new Exception("parameter " + parameter + " does not exist or is not defined");
        }
        addParam(KEY, value);
        return true;
    }

    public static void dumpEnvironment() {
        conf.dumpConfiguration();
    }

    public static void addParam(String pParamName, Object pValue) {
        synchronized (dynamicParams) {
            dynamicParams.put(pParamName, pValue);
        }
    }

    public static Object getParam(String pParamName) {
        return dynamicParams.get(pParamName);
    }
}
